#ifndef LENGTHVALIDATOR_H
#define LENGTHVALIDATOR_H

#include "stringvalidator.h"
#include "pretreatmenthandler.h"

class LengthValidator : public StringValidator
{
public:
    LengthValidator();
    LengthValidator(int min, int max = -1, const QString& errorMsg = "Invalid length")
        : m_min(min), m_max(max), m_errorMsg(errorMsg) {
        if (max < min && max != -1) {
            qWarning() << "LengthValidator: max < min, adjusting max to min";
            m_max = min;
        }
    }

    ValidationResult validate(const QString& input,
                              const std::vector<ValidationOption>& options = {}) const override {
        QString processed = PretreatmentHandler:: preprocess(input, options);
        const int len = processed.length();

        if (len < m_min) {
            return ValidationResult(false, m_errorMsg.arg(len).arg(m_min).arg(m_max));
        }
        if (m_max != -1 && len > m_max) {
            return ValidationResult(false, m_errorMsg.arg(len).arg(m_min).arg(m_max));
        }
        return ValidationResult(true);
    }

private:
    int m_min;
    int m_max;
    QString m_errorMsg;
};

#endif // LENGTHVALIDATOR_H
